<?php
namespace app\common\validate\merchant;
use app\common\model\Merchant\MerchantAccountModel;
use think\Validate;
/**
 * 商家充值记录验证器
 */
class MerchantRechargeValidate extends Validate
{
    // 验证规则
    protected $rule = [
        'ids' => ['require', 'array'],
        'id' => 'require',
        'is_disable' => ['require', 'in' => '0,1'],
        'amount'=> 'require',
        'setting_collection_code'=> 'require',
        'setting_collection_id'=> ['require'],
        'auth_status'=> 'require',
    ];
    // 错误信息
    protected $message = [
        'amount.require' => '请输入充值金额',
        'setting_collection_id.require' => '未获取到充值方式',
        'setting_collection_code.require' => '未获取到充值方式',
        'auth_status.require' => '请选择审核状态',
    ];
    // 验证场景
    protected $scene = [
        'info'    =>  ['id'],
        'add'     =>  ['amount','setting_collection_id','setting_collection_code'],
        'edit'    =>  ['id', ],
        'dele'    =>  ['ids'],
        'disable' =>  ['ids', 'is_disable'],
        'auth' =>  ['ids', 'auth_status'],
    ];
    //验证充值方式
    protected function checkExisted($value, $rule, $data = [])
    {
        $model = new MerchantAccountModel();
        $pk = $model->getPk();
        $id = $data[$pk] ?? 0;

        $where[] = [$pk, '=', $data['setting_collection_id']];
        $where[] = ['is_disable', '=',0];
        $where = where_delete($where);
        $info = $model->field($pk)->where($where)->find();
        if (!$info) {
            return '充值方式不存在';
        }

        return true;
    }
}
